package com.ferry.lock.engine;

import org.apache.commons.lang.StringUtils;

import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by daiyong
 */
public class LockHolder {
	private String lockNode;

	private AtomicInteger lockNums = new AtomicInteger(1);

	private int lockTime;

	public LockHolder(String lockNode) {
		this.lockNode = lockNode;
		this.lockTime = -1;
	}

	public LockHolder(int lockTime) {
		this.lockNode = StringUtils.EMPTY;
		this.lockTime = lockTime;
	}

	public void incLockNum() {
		this.lockNums.incrementAndGet();
	}

	public int decLockNum() {
		return this.lockNums.decrementAndGet();
	}

	public String getLockNode() {
		return lockNode;
	}

	public int getLockTime() {
		return lockTime;
	}

	/**
	 * 是否重入
	 * @return
	 */
	public static boolean isReentry(ThreadLocal<LockHolder> tl) {
		LockHolder lockHolder = tl.get();
		if (null != lockHolder) {
			lockHolder.incLockNum();
			return true;
		}
		return false;
	}
}
